<?php
namespace common\service;

use backend\models\AdminUser;
use common\models\ContralSet;
use common\models\UserOrderCount;
use moonland\phpexcel\Excel;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * 订单统计表
 * Class UserOrderCountServer
 * @package common\service
 */
class UserOrderCountServer {

    /**
     * 统计
     * @param $user_id
     * @param $type
     * @return array
     */
    public static function countOrder($user_id,$type,$on_time=null){
        if(empty($user_id)){
            return ["code"=>400,"msg"=>"用户ID不能为空"];
        }
        if(empty($type)){
            return ["code"=>400,"msg"=>"类型不能为空"];
        }
        if($type!="sign" && $type!="cancel" && $type!="exception" && $type!="exception_to_normal" && $type!="cancel_to_normal"){
            return ["code"=>400,"msg"=>"类型不合法"];
        }

        $curDate = date("Y-m-d") ;
        $curDateTime = date("Y-m-d H:i:s") ;

        $count = UserOrderCount::findOne(["count_date"=>$curDate , "user_id"=>$user_id]) ;
        if(empty($count)){
            if($type=="sign"){
                $sign = 1 ;
                $cancel = 0 ;
                $exception = 0 ;
            }
            if($type=="cancel"){
                $sign = 0 ;
                $cancel = 1 ;
                $exception = 0 ;
            }

            if($type=="exception"){
                $sign = 0 ;
                $cancel = 0 ;
                $exception = 1 ;
            }

            //从异常转到接收数据 理论上是进不来的
            if($type=="exception_to_normal"){
                $sign = 0 ;
                $cancel = 0 ;
                $exception = 0 ;
            }

            //从取消转到接收数据 理论上是进不来的
            if($type=="cancel_to_normal"){
                $sign = 0 ;
                $cancel = 0 ;
                $exception = 0 ;
            }

            $count = new UserOrderCount() ;
            $count->user_id = $user_id ;
            $count->count_date = $curDate ;
            $count->sign = $sign ;
            $count->cancel = $cancel ;
            $count->exception = $exception ;
            if($on_time==1){
                $count->on_time = 1 ;
            }
            if($on_time==2){
                $count->over_time = 1 ;
            }
            $count->upd_date = $curDateTime ;
            $count->save() ;
        }else{
            if($type=="sign"){
                $sign = $count['sign']+1 ;
                $count->sign = $sign ;
                if($on_time==1){
                    $count->on_time =$count['on_time']+1 ;
                }
                if($on_time==2){
                    $count->over_time = $count['over_time']+1 ;
                }
            }
            if($type=="cancel"){
                $cancel = $count['cancel']+1 ;
                $count->cancel = $cancel ;
            }
            if($type=="exception"){
                $exception = $count['exception']+1 ;
                $count->exception = $exception ;
            }

            //从异常转到接收数据
            if($type=="exception_to_normal"){
                $exception = $count['exception']-1 ;
                if($exception<0){
                    $exception = 0 ;
                }
                $count->exception = $exception ;
            }

            //从取消转到接收数据
            if($type=="cancel_to_normal"){
                $cancel = $count['cancel']-1 ;
                if($cancel<0){
                    $cancel = 0 ;
                }
                $count->cancel = $cancel ;
            }

            $count->upd_date = $curDateTime ;
            $count->save() ;
        }
        return ["code"=>200,"msg"=>"成功"];
    }
}